package com.bupt.liutong.front.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.admin.shop.bo.CartBo;
import com.bupt.liutong.admin.shop.form.CartForm;
import com.bupt.liutong.util.StringUtils;

public class CartAction extends BaseAction {
	private CartBo cartBo;
	
	public ActionForward insertCart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CartForm cartForm = (CartForm) form;
		UserForm sessionForm = (UserForm) request.getSession().getAttribute("userForm");
		cartForm.setUserName(sessionForm.getUserName());
		cartBo.insertCart(cartForm);
		return mapping.findForward("insertCart");
	}
	
	public ActionForward myCart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm sessionForm = (UserForm) request.getSession().getAttribute("userForm");
		if (sessionForm == null
				|| StringUtils.isEmpty(sessionForm.getUserName())) {
			ActionForward forward = mapping.findForward("login");
			String path = forward.getPath()+"?redirectUrl=/front/shop/cart.do?method=myCart";
			return new ActionForward(forward.getName(), path, forward.getRedirect(), forward.getContextRelative());
		} else {
			CartForm cartForm = (CartForm) form;
			cartForm.setUserName(sessionForm.getUserName());
			request.setAttribute("cartFormList", cartBo.getCarts(cartForm));
			return mapping.findForward("myCart");
		}
	}
	
	public ActionForward viewCourseOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm sessionForm = (UserForm) request.getSession().getAttribute("userForm");
		if (sessionForm == null
				|| StringUtils.isEmpty(sessionForm.getUserName())) {
			ActionForward forward = mapping.findForward("login");
			String path = forward.getPath()+"?redirectUrl=/front/shop/cart.do?method=viewCourseOrder";
			return new ActionForward(forward.getName(), path, forward.getRedirect(), forward.getContextRelative());
		} else {
			CartForm cartForm = (CartForm) form;
			cartForm.setUserName(sessionForm.getUserName());
			request.setAttribute("cartFormList", cartBo.getCarts(cartForm));
			return mapping.findForward("viewCourseOrder");
		}
	}

	public void setCartBo(CartBo cartBo) {
		this.cartBo = cartBo;
	}
}
